package de.jmda.mview.proj;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * Manages {@link ProjectModel} instances.
 *
 * @author roger.jmda@gmail.com
 */
// no namespace attribute here since the one defined in package-info will be
// used for this and other JAXB classes in this package
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProjectModelRepository
{
	/**
	 * Value is used in <code>package-info</code> and defines the XML namespace
	 * for this and other JAXB classes in this package.
	 */
	public final static String XML_NAMESPACE = "http://code.google.com/p/jmda/mview";
	
	public final static String DEFAULT_NAME = "default project model repository";

	@XmlAttribute(required=true)
	private String name;

	@XmlElementWrapper(name="projectModels")
  @XmlElements(@XmlElement(name="projectModel"))
	private Set<ProjectModel> projectModels = new HashSet<ProjectModel>();
	
	/**
	 * If available, the file that stores the project model repository is stored
	 * XLM transiently here.
	 */
	@XmlTransient
	private File file;

	/**
	 * make JAXB happy
	 * <p>
	 * preserve this constructor for JAXB, do not call other constructors with
	 * default values
	 */
	private ProjectModelRepository()
	{
		super();
	}

	public ProjectModelRepository(String name)
	{
		this();
		this.name = name;
	}

	public void add(ProjectModel projectModel)
	{
		projectModels.add(projectModel);
	}
	
	public void remove(ProjectModel projectModel)
	{
		projectModels.remove(projectModel);
	}
	
	public String getName()
  {
  	return name;
  }

	public File getFile()
  {
  	return file;
  }

	/**
	 * Set the file that stores the project model repository. Usually called by
	 * {@link ProjectModelRepositoryStore#store(File, ProjectModelRepository)}.
	 *
	 * @param file
	 */
	public void setFile(File file)
  {
  	this.file = file;
  }

	/**
	 * @return unmodifiable set
	 */
	public Set<ProjectModel> getProjectModels()
	{
		return Collections.unmodifiableSet(projectModels);
	}

	public static File createDefaultFile()
  {
	  return new File(
				ProjectModelRepository.class.getSimpleName() +
				ProjectModelRepositoryStore.FILE_EXTENSION);
  }
	
	public static ProjectModelRepository createDefaultProjectModelRepository()
	{
		ProjectModelRepository result = new ProjectModelRepository(DEFAULT_NAME);
		
		result.add(ProjectModel.createDefaultProjectModel());
		
		result.setFile(createDefaultFile());

		return result;
	}
}